<template>
  <div class="todo-list">
    <div class="todo-input">
      <input 
        v-model="newTodo" 
        @keyup.enter="addTodo"
        placeholder="请输入待办事项..."
      >
      <button @click="addTodo">添加</button>
    </div>

    <ul class="todo-items">
      <li v-for="todo in todos" :key="todo.id" :class="{ completed: todo.completed }">
        <input 
          type="checkbox" 
          v-model="todo.completed"
        >
        <span>{{ todo.text }}</span>
        <button @click="removeTodo(todo)">删除</button>
      </li>
    </ul>

    <div class="todo-stats" v-if="todos.length">
      <span>完成: {{ completedCount }} / {{ todos.length }}</span>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const newTodo = ref('')
const todos = ref([
  { id: 1, text: '学习 Vue 3', completed: false },
  { id: 2, text: '创建待办事项应用', completed: false }
])

const completedCount = computed(() => {
  return todos.value.filter(todo => todo.completed).length
})

const addTodo = () => {
  if (newTodo.value.trim()) {
    todos.value.push({
      id: Date.now(),
      text: newTodo.value,
      completed: false
    })
    newTodo.value = ''
  }
}

const removeTodo = (todo) => {
  const index = todos.value.indexOf(todo)
  todos.value.splice(index, 1)
}
</script>

<style scoped>
.todo-list {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.todo-input {
  display: flex;
  margin-bottom: 20px;
}

.todo-input input {
  flex: 1;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin-right: 10px;
}

button {
  background: #42b983;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background: #3aa876;
}

.todo-items {
  list-style: none;
  padding: 0;
}

.todo-items li {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
}

.todo-items li span {
  flex: 1;
  margin: 0 10px;
}

.completed span {
  text-decoration: line-through;
  color: #999;
}

.todo-stats {
  margin-top: 20px;
  color: #666;
  font-size: 0.9em;
}
</style> 